package com.mtw.bbs.auth.controller;


import com.mtw.bbs.auth.pojo.PayloadData;
import com.mtw.bbs.auth.utils.JwtTokenUtil;
import com.mtw.bbs.common.core.constant.AuthConstant;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.userAdmin.feign.ResourceClient;
import com.mtw.bbs.userAdmin.feign.UserClient;
import com.mtw.bbs.userAdmin.pojo.dto.UserDto;
import com.mtw.bbs.userAdmin.pojo.vo.UserInfoVo;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息
 */
@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController {



    private final UserClient userClient;
    private final JwtTokenUtil jwtTokenUtil;
    private final ResourceClient resourceClient;



    @GetMapping("/getInfo")
    @ApiOperation(value = "查询当前用户的基本信息", notes = "根据当前用户id查询用户基本信息")
    public Result getInfo(HttpServletRequest request) {

        String token = request.getHeader(AuthConstant.SERVICE_AUTH_KEY);
        PayloadData payloadData = jwtTokenUtil.getPayload(token);
        Result<UserInfoVo> userInfo = userClient.getUserVoByUsername(payloadData.getUsername());
        if (userInfo.isFail()){
            return Result.fail(userInfo.getCode(),userInfo.getMesg());
        }
        return Result.success(userInfo.getData());
    }



    @GetMapping("/getMenu")
    @ApiOperation(value = "查询当前用户拥有的菜单", notes = "根据当前用户id查询用户拥有的菜单")
    public Result getMenu(HttpServletRequest request) {

        String token = request.getHeader(AuthConstant.SERVICE_AUTH_KEY);
        PayloadData payloadData = jwtTokenUtil.getPayload(token);
        Result<UserDto> userInfo = userClient.getUserInfoByUsername(payloadData.getUsername());
        if (userInfo.isFail()){
            return Result.fail(userInfo.getCode(),userInfo.getMesg());
        }
        return resourceClient.getMenu(userInfo.getData().getId());
    }


    @GetMapping("/getButton")
    @ApiOperation(value = "查询当前用户拥有的按钮", notes = "根据当前用户id查询用户拥有的按钮")
    public Result getButton(HttpServletRequest request) {

        String token = request.getHeader(AuthConstant.SERVICE_AUTH_KEY);
        PayloadData payloadData = jwtTokenUtil.getPayload(token);
        Result<UserDto> userInfo = userClient.getUserInfoByUsername(payloadData.getUsername());
        if (userInfo.isFail()){
            return Result.fail(userInfo.getCode(),userInfo.getMesg());
        }
        return resourceClient.getButton(userInfo.getData().getId());
    }



    @GetMapping("/getRole")
    @ApiOperation(value = "查询当前用户拥有的角色", notes = "根据当前用户id查询用户拥有的角色")
    public Result getRole(HttpServletRequest request) {

        String token = request.getHeader(AuthConstant.SERVICE_AUTH_KEY);
        PayloadData payloadData = jwtTokenUtil.getPayload(token);
        Result<UserDto> userInfo = userClient.getUserInfoByUsername(payloadData.getUsername());
        if (userInfo.isFail()){
            return Result.fail(userInfo.getCode(),userInfo.getMesg());
        }
        return resourceClient.queryRoleByUser(userInfo.getData().getId());
    }





}
